<template>
<div>
    <el-dialog title="修改管理员" :visible.sync="showFlag" @close="closeDialog" width="40%">
      <el-form ref="editAdminForm" :model="editAdminForm" :rules="editAdminRules" label-width="80px" >
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="realName">
              <el-input v-model="editAdminForm.realName" placeholder="请输入管理员姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="editAdminForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="editAdminForm.idCard" placeholder="请输入管理员身份证号" :clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="editAdminForm.sex" placeholder="请选择管理员性别" :clearable=true style="width:100%">
                <el-option label="男" value="0"></el-option>
                <el-option label="女" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input v-model="editAdminForm.age" placeholder="请输入管理员年龄"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birth">
              <el-date-picker v-model="editAdminForm.birth" type="date" placeholder="请选择出生日期" format="yyyy-MM-dd"  value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="职务" prop="position">
              <el-input v-model="editAdminForm.position" placeholder="请输入管理员职务"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公寓名称" prop="aptName" >
              <el-input v-model="editAdminForm.aptName" placeholder="请输入管理员公寓名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item class="dialog-footer" align="center">
          <el-button type="success" @click="onSave('editAdminForm')">保 存</el-button>
          <el-button @click="onCancel('editAdminForm')">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
</div>
</template>

<script>
export default {
name: "editAdmin",
  data(){

    return{
      editAdminForm:{
        id:'',
        realName:'',
        username:'',
        sex:'',
        age:'',
        idCard:'',
        birth:'',
        position:'',
        aptName:''
      },
      msg: "我是子组件中的数据",
      showFlag:false,
      editAdminRules:{
        realName: [
          { required: true, message: '请输入管理员姓名', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ],
        username: [{required: true,message: '请输入用户名',trigger: 'blur'}],
        position:[{required: true,message: '请输入职务', trigger: 'blur'}],
        aptName:[{required: true,message: '请输入公寓名称', trigger: 'blur'}],
      }
    }
  },
  methods:{
    init(row){
      this.$nextTick(()=>{
        this.row=row
        this.getUserdetail(row)
      })
      this.showFlag=!this.showFlag
    },
    getUserdetail(row){
      this.editAdminForm=row
      this.editAdminForm.sex === 0 ? this.editAdminForm.sex='男' :this.editAdminForm.sex= '女'
    },
    //保存修改数据
    onSave(){
      this.$refs.editAdminForm.validate(async valid=>{
        if(valid){
          console.log(this.editAdminForm.sex)
          if(this.editAdminForm.sex === '男' || this.editAdminForm.sex === '0'){
            this.editAdminForm.sex = 0
          }else{
            this.editAdminForm.sex = 1
          }
          let params={
            id:this.editAdminForm.id,
            real_name:this.editAdminForm.realName,
            username:this.editAdminForm.username,
            sex:this.editAdminForm.sex,
            age:this.editAdminForm.age,
            id_card:this.editAdminForm.idCard,
            birth:this.editAdminForm.birth,
            position:this.editAdminForm.position,
            apt_name:this.editAdminForm.aptName
          }
          const {data:res}=await this.$http.post('/user/updateUserByEntity',params)
          if(res.code !==200) return this.$message.error("修改管理员数据失败")
          this.$message.success("修改管理员数据成功")
        }
      })
      this.$emit("editCallBack",this.msg)
      this.showFlag=false
    },
    //取消修改
    onCancel(){
      this.$refs.editAdminForm.resetFields()
      this.showFlag=false
    },
    //关闭对话框的回调
    closeDialog(){
      this.$refs.editAdminForm.resetFields()
    }
  }
}
</script>

<style scoped>

</style>